#include "adc2_rte.h"
#include "rte.h"
static ADC2_data adc2_d;
static uint16_t *ADC2_Values;

#define ADC_V 3.3f /*ADC参考电压*/
#define ADC_ACC 65535 /*ADC精度*/
#define ADC_VALUE_TO_V(ADC2_ch) (((float)adc2_d.ADC2_mean_value[ADC2_ch] / ADC_ACC) * ADC_V)
#define SYS_FROCK_V(ADC2_ch) ((25.1f / 5.1f) * ADC_VALUE_TO_V(ADC2_ch))
#define LOCK_SEN_V(ADC2_ch) (ADC_VALUE_TO_V(ADC2_ch) * 3 / 2)
/*
 * 函数介绍: ADC2初始化
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void ADC2_rte_init(void)
{
    ADC2_CH channel;
    adc2_d.ADC2_Collection_Count = 0;
    adc2_d.ADC2_init_sampling = ADC2_NO;
    for (channel = PA7_ADC2_INP7; channel < ADC2_PINMAX; channel++)
    {
        adc2_d.ADC2_mean_value[channel] = 0;
        adc2_d.ADC2_raw_value[channel] = 0;
    }
}

/*
 * 函数介绍: 返回初始化采集一次标致
 * 参数:
 * 返回值: {*}
 * 备注:
 */
ADC2_INT_flag_result ADC2_rt_init_sampling(void)
{
    return adc2_d.ADC2_init_sampling;
}

/*
 * 函数介绍: ADC2计算每次通道均值
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void ADC2_Calculate_mean_value(void)
{
    ADC2_CH channel;
    /*没到10000次退出*/
    if (ADC2_MAX != adc2_d.ADC2_Collection_Count)
    {
        return;
    }
    /*计算平均值*/
    for (channel = PA7_ADC2_INP7; channel < ADC2_PINMAX; channel++)
    {
        adc2_d.ADC2_mean_value[channel] = adc2_d.ADC2_raw_value[channel]/ADC2_MAX;
        adc2_d.ADC2_raw_value[channel] = 0;
    }
    //HAL_GPIO_TogglePin(GPIOD,Output9_O_PD0_Pin);
    adc2_d.ADC2_Collection_Count = 0;    /*清除采样次数*/
    adc2_d.ADC2_init_sampling = ADC2_OK; /*初始化采样完成一次*/
}

/*
 * 函数介绍: ADC2采样每个通道原始值10次
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void ADC2_Calculate_raw_value(void)
{
    ADC2_CH channel;
    /*ADC中断没采集完成则退出*/
    //if (ADC2_NO == Rt_adc_interrupt_flag())
    //{
        //return;
    //}

    ADC2_Values = Rt_ADC2_data();

    for (channel = PA7_ADC2_INP7; channel < ADC2_PINMAX; channel++)
    {
        /*将ADC2中断采样的原始值赋值到ADC2_raw_value*/
        adc2_d.ADC2_raw_value[channel] += ADC2_Values[channel];
    }
    adc2_d.ADC2_Collection_Count += 1;
    ADC2_Calculate_mean_value();     /*采样到10000次时开始计算每个通道平均值*/
    Set_adc_interrupt_flag(ADC2_NO); /*清除ADC标志，开始DMA采集*/
}

/*
 * 函数介绍: 返回系统电压值
 * 参数:
 * 返回值: {*}
 * 备注:计算公式：系统工作电压 = (25.1/5.1)*(ADC/ADC精度)*3.3
 */
float Rt_ADC2_CH_v(ADC2_CH ADC2_ch)
{
    float V_value = 0; /*ADC2_ch通道电压*/
    switch (ADC2_ch)
    {
    case PC0_ADC2_INP10:
        //V_value = (25.1 / 5.1) * ((float)adc2_d.ADC2_mean_value[ADC2_ch] / 65535) * 3.3f;
        V_value = SYS_FROCK_V(PC0_ADC2_INP10)+AD_V_COM;
        break;
    case PC1_ADC2_INP11:
        //V_value = ((float)adc2_d.ADC2_mean_value[ADC2_ch] / 65535) * 3.3f;
        V_value = ADC_VALUE_TO_V(PC1_ADC2_INP11);//+AD_V_COM
        break;
    case PC2_C_ADC2_INP0:
        //V_value = (25.1 / 5.1) * ((float)adc2_d.ADC2_mean_value[ADC2_ch] / 65535) * 3.3f;
        V_value = SYS_FROCK_V(PC2_C_ADC2_INP0)+AD_V_COM;
        break;
    case PC3_C_ADC2_INP1:
        /* code */
        break;
    case PA7_ADC2_INP7:
        //V_value = ((float)adc2_d.ADC2_mean_value[ADC2_ch] / 65535) * 3.3f * 3 / 2;/*Vad电压*/
        V_value = LOCK_SEN_V(PA7_ADC2_INP7);/*Vhl电压*/
        break;

    default:
        break;
    }
    return V_value;
}
